package cn.edu.swu;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class StudentDao {
    public void add(Student student) throws SQLException {
        QueryRunner queryRunner= new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "insert into student(StuNum,name,password) values(?,?,?)";
        queryRunner.update(sql,student.getStuNum(),student.getName(),student.getPassword());
    }
    public void delete(Integer id) throws SQLException{
        QueryRunner queryRunner= new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "delete from student where id=?";
        queryRunner.update(sql,id);
    }
    public void update(Student student) throws SQLException{
        QueryRunner queryRunner= new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "update student set StuNum=?,name =?,password=?,where id=?";
        queryRunner.update(sql,student.getStuNum(),student.getName(),student.getPassword(),student.getId());
    }
    public List<Student> list(Student student) throws SQLException{
        QueryRunner queryRunner= new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "select * from admin";
        List<Student> list =queryRunner.query(sql,new BeanListHandler<>(Student.class));
        return list;
    }
    public Student findById(Integer id) throws SQLException {
        QueryRunner queryRunner= new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "select * from student where id=?";
        Student student =queryRunner.query(sql,new BeanHandler<>(Student.class),id);
        return student;
    }
    public Student login(String stuNum,String password) throws SQLException {
        QueryRunner queryRunner= new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "select * from admin where StuNum=?,and password=?";
        Student student =queryRunner.query(sql,new BeanHandler<>(Student.class),stuNum,password);
        return student;
    }

}

